<template>
  <div class="app-container">
    <el-tabs type="border-card">

      <el-form :inline="true" :model="searchParam" class="demo-form-inline head-form">

        <el-form-item style="margin-top: 5px;" class="jySle">
          <el-input :placeholder="placeholderTxt" v-model="input5" class="input-with-select" size="small">
            <el-select v-model="selectType" slot="prepend" placeholder="请选择" size="small" @change="changeSelType">
              <!-- <el-option label="手机号" value="phone"></el-option> -->
              <el-option label="订单号" value="orderNo"></el-option>
              <!-- <el-option label="微信订单号" value="transactionId"></el-option> -->
              <!-- <el-option label="退款单号" value="refundNo"></el-option> -->
              <el-option label="销售微信号" value="wechatNum"></el-option>
            </el-select>
            <!-- <el-button slot="append" icon="el-icon-search"></el-button> -->
          </el-input>
        </el-form-item>
        <el-form-item>
          <span class="demonstration">日期筛选:</span>
          <el-date-picker v-model="orderDate" type="datetimerange" format="yyyy-MM-dd hh:mm:ss"
            value-format="yyyy-MM-dd hh:mm:ss" :picker-options="pickerOptions2" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期" align="right" size="small" @change='dateChange'>
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button :loading="tableLoading" @click="search" size="small" icon="el-icon-search" type="primary">搜索
          </el-button>
          <!--
          <el-button @click="memberInfo()" size="small" icon="el-icon-plus" type="primary">添加会员
          </el-button>
          <el-button icon="el-icon-date" @click="batchDel()" size="small" class="add">批量删除</el-button> -->
          <!-- <el-button icon="el-icon-setting" @click="batchCache()" size="small" class="add">重置缓存</el-button> -->
          <el-button @click="initSearchParam();search()" icon="el-icon-refresh-right" type="primary" size="small" style="background-color: #fff; border: 1px solid #ccc ; color: #666">清空
          </el-button>
        </el-form-item>
      </el-form>
      <div class="order_nav">
        <el-tabs v-model="searchParam.status" type="card" @tab-click="tabClick">
          <el-tab-pane label="全部订单" name="0"></el-tab-pane>
          <el-tab-pane label="待付款" name="1"></el-tab-pane>
          <el-tab-pane label="已付款" name="2"></el-tab-pane>
          <el-tab-pane label="退款中" name="5"></el-tab-pane>
          <el-tab-pane label="已退款" name="6"></el-tab-pane>
        </el-tabs>

      </div>
      <el-table stripe ref="table" :height="tableHeight" v-loading="tableLoading" :data="orderList" border row-key="id"
        @selection-change="handleSelectionChange">
        <el-table-column show-overflow-tooltip type="selection" align="center" />
        <el-table-column show-overflow-tooltip prop="userName" label="用户昵称" align="center" width="120" />
        <el-table-column show-overflow-tooltip prop="userImg" label="头像" align="center" width="80">
          <template slot-scope="{row}">
            <el-avatar :size="30" :src="row.userImg">
              <img :src="row.userImg||'https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png'" />
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="phone" label="手机号码" align="center" width="150" />
        <el-table-column show-overflow-tooltip prop="orderNo" label="订单号" align="center" width="255" />
        <el-table-column show-overflow-tooltip prop="payAmount" label="付款金额" align="center" width="100" >
        <template slot-scope="{row}">
            <div class=""  style="color: #409eff;">
                {{row.payAmount/100}}
            </div>
        </template>
        </el-table-column>

        <el-table-column show-overflow-tooltip prop="type" label="订单类型" align="center" width="180" >
        <template slot-scope="{row}">
           <el-tag v-if="row.type == 0" type="success" size="small">课程订单</el-tag>
           <el-tag v-if="row.type == 1" type="warning" size="small">会员订单</el-tag>
        </template>
        </el-table-column>


        <el-table-column show-overflow-tooltip prop="wechatNumStr" label="导师号" align="center" width="180" />
        <el-table-column show-overflow-tooltip prop="payTime" label="支付时间" align="center"  />
        <el-table-column show-overflow-tooltip prop="createTime" label="创建时间" align="center"  />
        <el-table-column show-overflow-tooltip prop="status" label="订单状态" align="center" width="150" fixed="right">
          <template slot-scope="{row}">
            <div>
              <el-tag v-if="row.status == 1" type="" size="small">待支付</el-tag>
              <el-tag v-if="row.status == 2" type="success" size="small">已支付</el-tag>
              <el-tag v-if="row.status == 3" type="warning" size="small">超时取消</el-tag>
              <el-tag v-if="row.status == 4" type="info" size="small">主动取消</el-tag>
              <el-tag v-if="row.status == 5" type="danger" size="small">退款中</el-tag>
              <el-tag v-if="row.status == 6" type="danger" size="small">退款成功</el-tag>
              <el-tag v-if="row.status == 7" type="danger" size="small">退款失败</el-tag>
            </div>
          </template>
        </el-table-column>
        <!--  <el-table-column v-if="isAdmin||(btnFunc.filter(value=>value=='edit'||value=='delete').length>0)" label="操作"
          width="120" align="center" fixed="right">
          <template slot-scope="scoped">
            <el-row>
              <el-tooltip v-if="isAdmin||btnFunc.some(btn=>btn=='edit')" class="item" effect="dark" content="编辑"
                placement="bottom">
                <el-button type="primary" size="mini" icon="el-icon-edit" @click="memberInfo(scoped.row)" />
              </el-tooltip>
              <el-tooltip v-if="!isAdmin||btnFunc.some(btn=>btn=='delete')" class="item" effect="dark" content="删除"
                placement="bottom">
                <el-button :loading="delBtnLoading" type="danger" size="mini" class="el-icon-delete" @click="memberDel(scoped.row)" />
              </el-tooltip>
            </el-row>
          </template>
        </el-table-column> -->
      </el-table>
      <!-- 分页 -->
      <div class="page-current">
        <el-pagination background :current-page="currentPage" :page-size="pageSize" prev-text="上一页" next-text="下一页"
          layout="total, prev, pager, next, jumper" style="display: inline-block;" :total="count" @current-change="handlePageChange" />
      </div>
    </el-tabs>



  </div>
</template>

<script>
  import orderApi from '@/api/gzh/order'
  import editForm from './edit'

  export default {
    name: "order",
    data() {
      return {
          pickerOptions2: {
                  shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                      const end = new Date();
                      const start = new Date();
                      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                      picker.$emit('pick', [start, end]);
                    }
                  }, {
                    text: '最近一个月',
                    onClick(picker) {
                      const end = new Date();
                      const start = new Date();
                      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                      picker.$emit('pick', [start, end]);
                    }
                  }, {
                    text: '最近三个月',
                    onClick(picker) {
                      const end = new Date();
                      const start = new Date();
                      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                      picker.$emit('pick', [start, end]);
                    }
                  }]
         },
        orderDate:'',
        placeholderTxt: '请输入订单号',
        input5: '',
        orderStatus: '0',
        tableLoading: false,
        // 当前页
        currentPage: 1,
        // 总条数
        count: 0,
        // 每页条数
        pageSize: 10,
        // 显示编辑角色弹框
        addBtnLoading: false,
        addDialogVisible: false,
        delBtnLoading: false,
        channelList: [],
        selectType: 'orderNo',
        searchParam: {
          orderNo: '', // 订单号
          transactionId: '', //微信支付单号
          status: '0', //订单状态
          phone: '', // 电话
          refundNo: '', //退款订单号
          wechatNum: '' ,//分配的销售微信
          startTime:'',
          endTime:''
        },
        status: [{
          value: 1,
          label: '正常'
        }, {
          value: 2,
          label: '禁用'
        }],
        orderList: [],
        deviceList: [],
        infoForm: {
          name: '',
          status: 1,
          device: 5,
          source: 2,
        },
        loading: false,
        tableHeight: document.body.clientHeight - 300,
        btnFunc: [],
        isAdmin: false,
        dialogTitle: "添加",
      }
    },
    components: {
      editForm
    },
    created() {
      const {
        btnFunc,
        isAdmin
      } = this.$store.state.user;
      console.log(this.$store.state.user)
      this.btnFunc = btnFunc
      this.isAdmin = isAdmin
      this.pageSize = parseInt((document.body.clientHeight - 395) / 50)
    },
    mounted() {
      // 开启加载动画
      this.tableLoading = true
      // 获取数据列表
      this.getOrderList()

    },
    watch: {},
    methods: {
    dateChange(e){
        console.log(e);
        var dateArr=e;
        this.searchParam.startTime=dateArr[0];
        this.searchParam.endTime=dateArr[1];


    },
      changeSelType(e) {

        if (e == 'phone') {
          this.placeholderTxt = '请输入手机号';
        } else if (e == 'orderNo') {
          this.placeholderTxt = '请输入订单号';
        } else if (e == 'transactionId') {
          this.placeholderTxt = '请输入微信订单号';
        } else if (e == 'refundNo') {
          this.placeholderTxt = '请输入退款订单号';
        } else {
          this.placeholderTxt = '请输入销售微信号';
        }



      },
      tabClick(e) {
        console.log(this.searchParam.status);
        var newStatus = this.searchParam.status;
        this.input5 = '';
        this.searchParam = {
          tradeNo: '', // 订单号
          transactionId: '', //微信支付单号
          phone: '', // 电话
          refundNo: '', //退款订单号
          status: newStatus,
          saleNum: '' //分配的销售微信
        }
        this.orderDate=';'
        this.tableLoading = true;
        this.getOrderList();
      },
      paramInput(data) {
        this.searchParam[data];
        for (var key in this.searchParam) {
          if (key != data) {
            this.searchParam[key] = '';
          }
        }

      },
      handleSelectionChange(row) {
        console.log(row)
      },
      batchDel() {
        console.log(this.$refs.table.clearSelection());
      },
      // 异步获取数据
      async queryData() {
        // 获取设备类型
        await this.getParamList()
      },
      // 初始化搜索条件
      initSearchParam() {
        this.searchParam = {
          endTime:"",
          startTime:'',
          orderNo: '', // 订单号
          transactionId: '', //微信支付单号
          status: '0', //订单状态
          phone: '', // 电话
          refundNo: '', //退款订单号
          wechatNum: '' //分配的销售微信
        }
        this.input5='';
        this.orderDate='';
      },
      // 获取数据列表
      getOrderList() {
        let searchParam = JSON.parse(JSON.stringify(this.searchParam))
        searchParam.pageIndex = this.currentPage
        searchParam.pageSize = this.pageSize
        orderApi.orderList(searchParam).then(res => {
          this.orderList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(e => {
          this.orderList = []
          this.tableLoading = false
        })
      },
      // 初始化表单
      initInfoForm() {
        this.infoForm = {
          mobile: '',
          status: 1,
          device: 5,
          source: 2,
          gender: 1,
        }
      },
      // 删除记录
      memberDel(data) {
        this.$confirm('您确定要删除级别吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delBtnLoading = true
          orderApi.memberDel({
            id: data.id
          }).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
            this.delBtnLoading = false
            this.tableLoading = true
            this.getOrderList()
          }).catch(e => {
            this.delBtnLoading = false
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 获取参数
      getParamList() {
        orderApi.getParamList().then(res => {
          const {
            device_list
          } = res.data
          this.deviceList = device_list ? device_list : {}
        }).catch(e => {
          this.deviceList = {}
        })
      },
      handlePageChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getOrderList()
      },
      // 搜索
      search() {
        console.log(this.orderDate)
        for (var key in this.searchParam) {
          if (key != 'status'&&key != 'endTime'&&key != 'startTime') {
            this.searchParam[key] = ''
          }
        }

        this.searchParam[this.selectType] = this.input5
        this.currentPage = 1
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getOrderList()
      },
      // 提交表单后逻辑处理
      submit(type) {
        if (type == 0) {
          // 添加
          this.search()
        } else {
          // 编辑
          this.tableLoading = true
          this.getOrderList()
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.addDialogVisible = false
        }
      },
      // 关闭窗体
      closeWin() {
        console.log("关闭")
        // 调用初始化表单方法
        this.initInfoForm()
        // 设置弹窗不可见
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .jySle .el-select .el-input {
    width: 100px;
  }

  .jySle .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .jySle .input-with-select {
    width: 350px;
  }

  .app-container {
    width: 100%;

    .page-current {
      text-align: center;
      padding: 15px 5px;
      padding-bottom: 0px;

      .sure-page-btn {
        display: inline-block;
        height: 28px;
        line-height: 0;
        width: 46px;
        padding: 0;
        vertical-align: top;
      }
    }
  }
</style>
